/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.edu.hzjt.dao.JkyContributionAttMapper;
import cn.qidisoft.edu.hzjt.dao.JkyContributionMapper;
import cn.qidisoft.edu.hzjt.model.JkyContribution;
import cn.qidisoft.edu.hzjt.model.JkyContributionAtt;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * @author hxq
 * @version $Id: IJkyContributionAttService.java, v 0.1 2018年12月6日 上午10:42:10 hxq Exp $
 */
@Service("jkyContributionAttService")
public class IJkyContributionAttServiceImpl
    implements cn.qidisoft.edu.hzjt.service.IJkyContributionAttService {
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);

  @Autowired
  private JkyContributionAttMapper jkyContributionAttMapper;
  @Autowired
  private JkyContributionMapper jkyContributionMapper;

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionAttService#deleteByPK(java.lang.Integer)
   */
  @Override
  @Transactional
  public int deleteByPK(JkyContributionAtt jkyContributionAtt) {
    try {
      // 删除附件，主表的投稿数量相对应的减一
      int num = jkyContributionAttMapper.deleteByPK(jkyContributionAtt.getFileid());
      if (num >= 1) {
        num = 0;
        JkyContribution jkyContribution = new JkyContribution();
        jkyContribution.setTgid(jkyContributionAtt.getParentid());
        num = jkyContributionMapper.updateByDocNum(jkyContribution);
      }
      return num;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("deleteByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(jkyContributionAtt, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionAttService#insert(cn.qidisoft.edu.hzjt.model.JkyContributionAtt)
   */
  @Override
  @Transactional
  public int insert(JkyContributionAtt record) {
    return 0;
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionAttService#queryByPK(java.lang.Integer)
   */
  @Override
  public JkyContributionAtt queryByPK(Integer fileid) {
    try {
      return jkyContributionAttMapper.queryByPK(fileid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(fileid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionAttService#updateByPK(cn.qidisoft.edu.hzjt.model.JkyContributionAtt)
   */
  @Override
  @Transactional
  public int updateByPK(JkyContributionAtt record) {
    return 0;
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionAttService#deleteByParentIdPK(java.lang.Integer)
   */
  @Override
  @Transactional
  public int deleteByParentIdPK(Integer parentid) {
    try {
      return jkyContributionAttMapper.deleteByParentIdPK(parentid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("deleteByParentIdPK [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(parentid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IJkyContributionAttService#queryContributionAttList(java.lang.Integer)
   */
  @Override
  public List<JkyContributionAtt> queryContributionAttList(Integer tgid) {
    try {
      JkyContributionAtt paramObj = new JkyContributionAtt();
      paramObj.setParentid(tgid);
      return jkyContributionAttMapper.queryContributionAttList(paramObj);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryContributionAttList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(tgid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

}
